const express = require("express");
var router = express.Router();
// const axios = require("axios");
var QRCode = require('qrcode')

let xlsx = require("node-xlsx")
let { client } = require("../data/config")
//引入
/* 显示所有数据库 */
let list = []
router.post("/previewUI", (req, res) => {
  var { code } = req.body
  list.push(req.body)
  var url = `${client}${code}`
  // var url = `${client}/view?code=${code}`
  QRCode.toDataURL(url, function (err, image) {
    res.send({
      msg: "成功",
      data: image,
      url
    })
  })
});

router.get("/getUIData", (req, res) => {
  var { code } = req.query
  var result = list.find(el => {
    if (el.code === code)
      return el
  })
  res.send({
    msg: "成功",
    data: result ?? "没有数据"
  })
})

router.post("/infoToExcel", (req, res) => {
  let data = req.body
  console.log(data);
  let arr = []
  arr.push(data)
  res.send(xlsx.build(arr))
})

/* 用户上传信息，进行评分 */
router.post("/uploadUserForm", (req, res) => {
  let data = req.body
  var totalScore = 0
  const findTree = (arr) => {
    arr.forEach(item => {
      if (item.correctValue === item.value) {
        totalScore += item.score
      }
      if (item.children.length > 0) {
        findTree(item.children)
      }
    })
  }

  res.send({
    data: totalScore,
    msg: "成功"
  })
})
/* 获取模板 */
router.get("/templateList", (req, res) => {

})
module.exports = router;
